# -*- coding: utf-8 -*-
"""
-------------------------------------------------
# @Project  :pythonProject
# @File     :violator_shoot
# @Date     :2021/8/26 17:41
# @Author   :zhumenglong
# @Software :PyCharm
-------------------------------------------------
"""
import math

from wareHouse import *

class Shoot:

    def __init__(self,file_path,Index_path,goal_path):
        self.file_path = file_path
        self.Index_path = Index_path
        self.goal_path = goal_path

    def draw_frame(self):
        with open(self.Index_path,'r',encoding='utf-8') as fp:
            while True:
                file_name = fp.readline()[:-1]
                if len(file_name) > 0:
                    json_name = self.file_path+file_name+'.json'
                    pict_name = self.file_path+file_name+'.jpg'
                    with open(json_name,'rb') as f:
                        # f.read()
                        json_info = json.loads(f.read())
                        img_info = method.cv2_imread(pict_name,-1)
                        phone_mid_x, phone_mid_y = 0, 0
                        nose_x, nose_y = 0, 0
                        nose_listx = []
                        nose_listy = []
                        for i in json_info:
                            if i == "person":
                                cat_name = ""
                                for num in json_info[i]:
                                    for j in num:
                                        if j == "box":
                                            cat_name = "person"
                                            xmin = int(num[j]['x'])
                                            ymin = int(num[j]['y'])
                                            xmax = int(num[j]['x']) + int(num[j]['width'])
                                            ymax = int(num[j]['y']) + int(num[j]['height'])
                                            # conf = num[j]['conf']

                                            x = 150
                                            y = 0
                                            z = 0

                                            color = (x, y, z)
                                            cv2.rectangle(img_info, (int(xmin), int(ymin)), (int(xmax), int(ymax)),
                                                          color, 2)
                                            font = cv2.FONT_HERSHEY_SIMPLEX
                                            cv2.putText(img_info, cat_name, (int(xmin), int(ymin)), font, 1, color, 2)

                                        else:

                                            keypoint_thresh_vis = 0.3

                                            keypoint = []
                                            for ii in num[j]:
                                                keypoint.append(ii)
                                            keypoint = np.array(keypoint)
                                            # 用来保存鼻子的坐标，
                                            nose_listx.append(int(keypoint[0]))
                                            nose_listy.append(int(keypoint[1]))
                                            # nose_x = int(keypoint[0])
                                            # nose_y = int(keypoint[1])

                                            keypoint = keypoint.reshape((1, -1, 3))
                                            joint_visible = keypoint[:, :, 2] > keypoint_thresh_vis

                                            kpts_num = keypoint.shape[1]
                                            for i1 in range(keypoint.shape[0]):
                                                pts = keypoint[i1]
                                                for color_i, jp in zip(colors, main_joint_pairs):
                                                    if jp[0] >= kpts_num or jp[1] >= kpts_num: continue
                                                    if joint_visible[i1, jp[0]] and joint_visible[i1, jp[1]]:
                                                        pt0 = pts[jp, 0]
                                                        pt1 = pts[jp, 1]
                                                        pt0_0, pt0_1, pt1_0, pt1_1 = int(pt0[0]), int(pt0[1]), int(
                                                            pt1[0]), int(pt1[1])

                                                        cv2.line(img_info, (pt0_0, pt1_0), (pt0_1, pt1_1), color_i, 3)

                            elif i == "phone":
                                cat_name = "phone"
                                for num in json_info[i]:
                                    xmin = int(num['x'])
                                    ymin = int(num['y'])
                                    xmax = int(num['x']) + int(num['width'])
                                    ymax = int(num['y']) + int(num['height'])
                                    phone_mid_x = xmin + int(num['width']) // 2
                                    phone_mid_y = ymin + int(num['height']) // 2
                                    print(phone_mid_x,phone_mid_y)

                                    color = (0, 0, 250)
                                    cv2.rectangle(img_info, (int(xmin), int(ymin)), (int(xmax), int(ymax)), color, 2)
                                    font = cv2.FONT_HERSHEY_SIMPLEX
                                    cv2.putText(img_info, cat_name, (int(xmin), int(ymin)), font, 1, (0, 0, 250), 2)

                                    # 鼻子到手机中心点的一条线
                                    for kx, ky in zip(nose_listx, nose_listy):
                                        print(kx,ky)
                                        if math.fabs(phone_mid_x - kx) < 200 and math.fabs(phone_mid_y - ky) < 200:
                                            k = (phone_mid_y - ky) / (phone_mid_x - kx)
                                            b = (ky * phone_mid_x - (phone_mid_y * kx)) / (phone_mid_x - kx)
                                            ux =int(math.fabs(b / k))
                                            uy = 0

                                            dx = int((1080 - b) / k)
                                            dy = 1080
                                            cv2.line(img_info, (kx, ky), (int(dx), int(dy)),
                                                     (100, 100, 250), 6)
                                        else:
                                            continue

                            elif i == "screen":
                                cat_name = "screen"
                                for num in json_info[i]:
                                    xmin = int(num['x'])
                                    ymin = int(num['y'])
                                    xmax = int(num['width']) + xmin
                                    ymax = int(num['height']) + ymin
                                    color = (0,250,0)
                                    cv2.rectangle(img_info, (xmin, ymin), (xmax, ymax), color, 2)
                                    font = cv2.FONT_HERSHEY_SIMPLEX
                                    cv2.putText(img_info, cat_name, (xmin, ymin), font, 1, (0, 250, 0),2)

                            else:
                                cat_name = str(json_info[i])
                                font = cv2.FONT_HERSHEY_SIMPLEX
                                cv2.putText(img_info, cat_name, (1600, 100), font, 2, (0,200,0), 2)

                        method.create_path(self.goal_path)

                        method.cv2_imwrite_jpg("./" + self.goal_path + "/" + file_name + ".jpg", img_info)
                else:
                    break

